import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_plugin_eth_wallet/walletUtils.dart';
import 'package:flutter_plugin_eth_wallet/contractUtils.dart';
//import 'package:flutter_plugin_eth_wallet/walletUtils.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    await WalletUtils.import(
        "inquiry lonely seminar boost sentence lounge volcano together ecology mammal awake clarify",
//        "isolate employ thought adult resemble vague ecology ceiling puzzle spirit sheriff become",
        "123");
    await ContractUtils.config("https://rinkeby.infura.io/",
        "0x8cc96dfe394def64659e47df7e048c5ee285bd57", "CTT");
    var result = await ContractUtils.transfer(
        "123", "0xb94E17C8E9F549d69d0457e784D38b938927494e", 9);
    var a = await ContractUtils.balance();
    var platformVersion = a.toString() + result.errorMessage;
    // Platform messages may fail, so we use a try/catch PlatformException.
//    try {
//      platformVersion = await WalletUtils.import(
//          "divorce dinner thought clinic deal nothing dish oxygen shrug bottom pottery celling",
//          "123");
//      var a = await ContractUtils.balance("fdshjfkdhsjkfhds");
//      platformVersion = a.toString();
////      platformVersion = await WalletUtils.import("divorce dinner thought clinic deal nothing dish oxygen shrug bottom pottery celling", "123");
//    } on PlatformException {
//      platformVersion = 'Failed to get platform version.';
//    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
            child: Column(
          children: <Widget>[
            GestureDetector(
              onTap: () async {
                await WalletUtils.import(
                    "isolate employ thought adult resemble vague ecology ceiling puzzle spirit sheriff become",
                    "123");
              },
              child: Text("导入钱包" + _platformVersion),
            )
          ],
        )),
      ),
    );
  }
}
